﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Configuration;
using System.Data;
namespace PracticeWcf
{
    public class cityData
    {
        //public List<CityRequest> requestedCityList = new List<CityRequest>();
        private string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
       //// internal CityRequest[] getCity(int valid)
       // {
          
  
            //SqlConnection connection = GetConnection();
            //string sqlcommand = "SELECT ID, City, valid FROM Location";
            //SqlCommand command = connection.CreateCommand();
            //command.CommandText = sqlcommand;
           
            //SqlDataReader reader = null;
           
            //try
            //{
                
            //    reader = command.ExecuteReader(/*CommandBehavior.CloseConnection*/);
                
            //    while (reader.Read())
            //    {
            //        CityRequest cityRequests = new CityRequest();
            //        cityRequests.ID = Convert.ToInt16(reader[0]);
            //        cityRequests.City = Convert.ToString(reader[1]);
            //        cityRequests.valid = Convert.ToInt16(reader[2]);
            //        requestedCityList.Add(cityRequests);
            //    }
            //}

            //finally
            //{
            //    if (reader != null)
            //    {
            //        reader.Close();
            //        reader.Dispose();
            //    }

            //    if (connection != null)
            //    {
            //        connection.Close();
            //        connection.Dispose();
            //    }
            //}
       //     return requestedCityList.ToArray();

       // }

            


            public List<City> GetCities()
            {
                List<City> cityList = new List<City>();
                SqlConnection con = GetConnection();
                string query = "SELECT ID, City FROM Location";
                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    City city = new City();
                    city.ID = Convert.ToInt32(dr["ID"]);
                    city.cityName = dr["City"].ToString();
                    

                   cityList.Add(city); 
                }
                return cityList;

            }

           


           
            


        private SqlConnection GetConnection()
        {
            SqlConnection connection = new SqlConnection(connectionString);
            try
            {
                connection.Open();
            }
            finally
            {
            }
            return connection;
        }
    }
}